package com.yanqu.road.server.manger.activity.recycle;

import com.yanqu.road.entity.activity.ActivityItemRecycleData;
import com.yanqu.road.entity.config.goods.GoodsInfo;
import com.yanqu.road.entity.enums.eActivityType;
import com.yanqu.road.entity.enums.eMailType;
import com.yanqu.road.entity.player.UserBagItem;
import com.yanqu.road.logic.activity.qixi.QixiConfig;
import com.yanqu.road.server.logic.mail.MailManager;
import com.yanqu.road.server.manger.MultipleLanguageMgr;
import com.yanqu.road.server.manger.ServerLanguageMgr;
import com.yanqu.road.server.manger.activity.qixi.QixiMgr;
import com.yanqu.road.server.manger.config.GoodsMgr;
import com.yanqu.road.server.manger.player.UserMgr;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;
import com.yanqu.road.utils.string.StringUtils;

import java.math.BigInteger;
import java.text.MessageFormat;
import java.util.ArrayList;

public class QixiActivityRecycleStrategy extends AbstractItemRecycleStrategy {

    @Override
    public void markNeedRecycleItem() {
        QixiConfig config = QixiMgr.getConfig();
        if (config != null) {
            long recycleTime = config.getActivityInfo().getEndTime() * 1000L + 300 * 1000;//领奖期回收

            if(config.getActivityInfo().getChildType() == 1) {
                ActivityItemRecycleMgr.addRecycleItemData(config.getQIXI_PINK_ROSE_ID(), config.getActivityInfo().getType(), 2, recycleTime, new ArrayList<>());
                ActivityItemRecycleMgr.addRecycleItemData(config.getQIXI_RED_ROSE_ID(), config.getActivityInfo().getType(), 2, recycleTime, new ArrayList<>());
                ActivityItemRecycleMgr.addRecycleItemData(config.getQIXI_ROMANTIC_ROSE_ID(), config.getActivityInfo().getType(), 2, recycleTime, new ArrayList<>());

                long giftRecycleTime = config.getActivityInfo().getEndShowTime() * 1000L + 300 * 1000;//展示期结束回收

            }else if(config.getActivityInfo().getChildType() >= 2){
                //百福具臻  换皮
                addRecycleItemData(config.getQIXI_PINK_ROSE_ID(), config, recycleTime);
                addRecycleItemData(config.getQIXI_RED_ROSE_ID(), config, recycleTime);
                addRecycleItemData(config.getQIXI_ROMANTIC_ROSE_ID(), config, recycleTime);

            }

        }
    }

    private void addRecycleItemData(int itemId, QixiConfig config, long recycleTime) {
        ArrayList<String> paramList = new ArrayList<>();
        paramList.add(String.valueOf(itemId));
        //回收的道具
        GoodsInfo goodsInfo = GoodsMgr.getGoodsById(itemId);
        if(goodsInfo != null){
            //转化的道具，配置在（回收的道具）参数2
            paramList.add(goodsInfo.getParamList().get(1).toString());
            //单位换算，配置在（回收的道具）参数3
            paramList.add("2");
            paramList.add(config.getActivityInfo().getActivityName());
            ActivityItemRecycleMgr.addRecycleItemData(itemId, config.getActivityInfo().getType(), 3, recycleTime, paramList);
        }
    }

    @Override
    protected void initActivityType() {
        activityType = eActivityType.QiXiActivity.getValue();
    }

    @Override
    protected void doRecycle(long userId, ActivityItemRecycleData data) {
        int recycleType = data.getRecycleType();
        if(recycleType == 3){
            //换皮的回收，注意！！！！！！！
            item2Score(userId, data);
            return;
        }
        String language = UserMgr.getUserInfo(userId).getLanguage();
        UserBagItem userBagItem = getUserBagItem(userId, data.getItemId());
        if (userBagItem != null && userBagItem.getNum() > 0) {
            long num = userBagItem.getNum();
            if (removeItem(userBagItem) && recycleType != 0) {
                //物品类别161 花满七夕道具  参数列表：送花数|戒指id|戒指数量|每日赠送上限|是否广播,   赠送上限-1表示无限制，0表示不广播，1广播
                // 转换为道具
                GoodsInfo goodsInfo = GoodsMgr.getGoodsById(data.getItemId());
                Property property = new Property();
                if (recycleType == 2) {
                    int rewardId = goodsInfo.getParamList().get(1).intValue();
                    BigInteger rewardNum = goodsInfo.getParamList().get(2).multiply(BigInteger.valueOf(num));
                    property.addProperty(rewardId, rewardNum);
                }
                //  发邮件
                String removeName = ServerLanguageMgr.getContent(goodsInfo.getGoodsName(), language);
                String mailTitle = MultipleLanguageMgr.getContent(MailManager.QIXI_MAIL_TITLE, language);
                StringBuilder removeStr = new StringBuilder();
                removeStr.append(removeName).append("X").append(num);
                String mailContent = StringUtils.format(MultipleLanguageMgr.getContent(MailManager.QIXI_MAIL_CONTENT,
                        language), removeStr.toString());
                MailManager.sendMail(userId, eMailType.SYSTEM.getValue(), PropertyHelper.parsePropertyToString(property), mailContent, mailTitle);
            }
        }
    }

    @Override
    public boolean isActivityRunning() {
        return false;
    }
}
